#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include "Log.h"
#include "Constants.h"
#include "Grid.h"

Log::Log(const char* f_name, const char* dir) {
    char f_path[MAX_FILE_PATH_LENGTH];
    char command[MAX_FILE_PATH_LENGTH];
    strcpy(command, "rm -r ");
    strcat(command, dir);
    system(command);
    strcpy(command, "mkdir ");
    strcat(command, dir);
    system(command);
    
    its_dir = dir;
    its_ext = EXT_FILE_NAME;
    strcpy(f_path, its_dir);
    strcat(f_path, f_name);
    strcat(f_path, its_ext);
    its_path = f_path;
    its_file_name = fopen(its_path, "wb+");
    fprintf(its_file_name, "-- start log --\n");
    cout << "-- start log --\n";
    
};

void Log::writeMsg(const char* str) {
    fprintf(its_file_name, "Msg: %s\n", str);
    cout << "Msg: " << str << "\n";
}
void Log::writeErr(const char* str) {
    fprintf(its_file_name, "Err: %s\n", str);
    cout << "Err: " << str << "\n";
}

void Log::writeInFile(const char* f_name, float** t, Grid* grid) {
    FILE* file;
    char f_path[MAX_FILE_PATH_LENGTH];
    strcpy(f_path, its_dir);
    strcat(f_path, f_name);
    strcat(f_path, its_ext); 
    file = fopen(f_path, "wb+");
    int num_r = grid->getNumberR();
    int num_z = grid->getNumberZ();
    float left_r = grid->getLeftR();
    float down_z = grid->getDownZ();
    float h_r = grid->getHR();
    float h_z = grid->getHZ();
    float r, z;

    for (int j = 0; j < num_z; j++) {
        for (int i = 0; i < num_r; i++) {
            fprintf(file, "%f %f %10.10f\n", grid->getPointR(i), grid->getPointZ(j), t[i][j]);
           // cout << "\n" << t[i][j];
        }

    }
    fclose(file);
}

void Log::writeInFile(const char* f_name, Matrix m) {
    FILE* file;
    char f_path[MAX_FILE_PATH_LENGTH];
    strcpy(f_path, its_dir);
    strcat(f_path, f_name);
    strcat(f_path, its_ext);
    file = fopen(f_path, "wb+");

    float h_r, h_z = 0;
    h_r = psi->h_r;
    h_z = psi->h_z;

    for (int j = 0; j < m.ColNo(); j++) {
        for (int i = 0; i < m.RowNo(); i++) {
            fprintf(file, "%f %f %10.10f\n", ppmts->getPlasmaLeftR() + i*h_r, ppmts->getPlasmaDownZ() + j*h_z, m(i,j));
          
        }
    }
    fclose(file);

};

const char* Log::getFilePath(){ return its_path; }
Log::~Log() {
    fprintf(its_file_name, "-- end log --\n");
    fclose(its_file_name);
    cout << "-- end log --\n";
}

void Log::writeInFile(const char* f_name, int iter_num, Matrix m) {
    FILE* file;
    char f_path[MAX_FILE_PATH_LENGTH];
    char s_iter_num[10] = "";
    sprintf(s_iter_num, "%d", iter_num);
    strcpy(f_path, its_dir);
    strcat(f_path, f_name);
    strcat(f_path, s_iter_num);
    strcat(f_path, its_ext);

    file = fopen(f_path, "wb+");

    float h_r, h_z = 0;
    h_r = psi->h_r;
    h_z = psi->h_z;

    for (int j = 0; j < m.ColNo(); j++) {
        for (int i = 0; i < m.RowNo(); i++) {
            fprintf(file, "%f %f %10.10f\n", ppmts->getPlasmaLeftR() + i*h_r, ppmts->getPlasmaDownZ() + j*h_z, m(i, j));
        }
    }

    fclose(file);

};

void Log::writeInFile(const char* f_name, int iter_num, float** t, Grid* grid) {
    FILE* file;
    char f_path[MAX_FILE_PATH_LENGTH];
    char s_iter_num[10] = "";
    sprintf(s_iter_num, "%d", iter_num);
    strcpy(f_path, its_dir);
    strcat(f_path, f_name);
    strcat(f_path, s_iter_num);
    strcat(f_path, its_ext);
    file = fopen(f_path, "wb+");
    int num_r = grid->getNumberR();
    int num_z = grid->getNumberZ();
    float left_r = grid->getLeftR();
    float down_z = grid->getDownZ();
    float h_r = grid->getHR();
    float h_z = grid->getHZ();
    float r, z;

    for (int j = 0; j < num_z; j++) {
        for (int i = 0; i < num_r; i++) {
            fprintf(file, "%f %f %10.10f\n", grid->getPointR(i), grid->getPointZ(j), t[i][j]);
         
        }

    }
    fclose(file);
}